package com.tencent.sr.iris.basic.saas.restapi;

import com.tencent.sr.iris.basic.saas.service.commonkv.IrisCommonKvConfigService;
import com.tencent.sr.iris.basic.service.interfaces.dto.TCommonKvConfigDTO;
import com.tencent.sr.iris.basic.service.interfaces.request.TCommonKvConfigAddRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.TCommonKvConfigBatchQueryRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.TCommonKvConfigDeleteRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.TCommonKvConfigQueryPageRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.TCommonKvConfigUpdateRequest;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import com.tencent.sr.rmall.springbootstarter.authtoken.IgnoreAuth;
import com.tencent.sr.rmall.springbootstarter.authtoken.localbean.AppUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@Api("KV配置服务")
@RequestMapping("/iris/basic/saas/v1/commonKV")
public class IrisCommonKvConfigController{

    @Resource
    private IrisCommonKvConfigService irisCommonKvConfigService;
    @Resource
    private AppUser appUser;

    /**
     * "新增kv配置（B端调用）
     * @param request
     * @return
     */
    @ApiOperation("新增kv配置（B端调用）")
    @PostMapping("/add")
    public HttpResult<Boolean> add(@Validated @RequestBody TCommonKvConfigAddRequest request) {
        request.setOperateId(appUser.getUid());
        return irisCommonKvConfigService.add(request);
    }

    /**
     * 更新kv配置（B端调用）
     * @param request
     * @return
     */
    @ApiOperation("更新kv配置（B端调用）")
    @PostMapping("/update")
    public HttpResult<Boolean> update(@Validated @RequestBody TCommonKvConfigUpdateRequest request) {
        request.setOperateId(appUser.getUid());
        return irisCommonKvConfigService.update(request);
    }

    /**
     * 删除kv配置（B端调用）
     * @param request
     * @return
     */
    @ApiOperation("删除kv配置（B端调用）")
    @PostMapping("/delete")
    public HttpResult<Boolean> delete(@Validated @RequestBody TCommonKvConfigDeleteRequest request) {
        request.setOperateId(appUser.getUid());
        return irisCommonKvConfigService.delete(request);
    }

    /**
     * 查询kv配置分页列表 toB
     *
     * @param page
     * @return
     */
    @ApiOperation("查询kv配置分页列表（B端使用）")
    @PostMapping("/queryCommonKVPage")
    public HttpResult<PaginationResponse<TCommonKvConfigDTO>> queryCommonKVPage(@Validated @RequestBody
            PaginationRequest<TCommonKvConfigQueryPageRequest> page) {
        page.getQueryCriteria().setOperateId(appUser.getUid());
        return irisCommonKvConfigService.queryCommonKVPage(page);
    }

}
